package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyOrderBean;
import com.zsyh.jiujiu.bean.WEiXInPayBean;
import com.zsyh.jiujiu.bean.XiangqingBean;
import com.zsyh.jiujiu.conference.chat.ChatActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.BigDecimalUtil;
import com.zsyh.jiujiu.utils.DateUtils;
import com.zsyh.jiujiu.utils.ToastUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class OrderInfoActivitty extends BaseActivity {

    @BindView(R.id.reinfoback)
    RelativeLayout reinfoback;
    @BindView(R.id.imagesttate)
    ImageView imagesttate;
    @BindView(R.id.sattte)
    TextView sattte;
    @BindView(R.id.daojishi)
    TextView daojishi;
    @BindView(R.id.wuliuyi)
    TextView wuliuyi;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.adress)
    TextView adress;
    @BindView(R.id.re_adress)
    RelativeLayout reAdress;
    @BindView(R.id.ordercode)
    TextView ordercode;
    @BindView(R.id.orderdeesc)
    TextView orderdeesc;
    @BindView(R.id.xian)
    View xian;
    @BindView(R.id.goodlist)
    RecyclerView goodlist;
    @BindView(R.id.bntright)
    TextView bntright;
    @BindView(R.id.bnttleft)
    TextView bnttleft;
    @BindView(R.id.restate)
    RelativeLayout restate;
    @BindView(R.id.xinxi)
    RelativeLayout xinxi;
    @BindView(R.id.wuliuer)
    TextView wuliuer;
    @BindView(R.id.outname)
    TextView outname;
    @BindView(R.id.outtphone)
    TextView outtphone;
    @BindView(R.id.outadress)
    TextView outadress;
    @BindView(R.id.re_outtadress)
    RelativeLayout reOuttadress;
    @BindView(R.id.chantime)
    TextView chantime;
    private String id, data, userId;

    String xiaoshi;
    String fenzhong;

    private BaseQuickAdapter<MyOrderBean.DataBean.JjyhOrderGoodsListBean, BaseViewHolder> mGoodAdapter;
    List<MyOrderBean.DataBean.JjyhOrderGoodsListBean> list = new ArrayList<>();
    MyOrderBean.DataBean goobean;
    private String time;
    private IWXAPI api;
    private String ingongsi, inwuliuid, ingongsibainam, outgongsi, outwuliid, outgongsibianma;

    @Override
    protected int initLayout() {
        return R.layout.activity_orderinfo;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        api = WXAPIFactory.createWXAPI(thisActivity, null);
        userId = (String) SPUtils.get(thisActivity, "userid", "");
        id = getIntent().getStringExtra("id");
        data = getIntent().getStringExtra("data");
        Gson gson = new Gson();
        goobean = gson.fromJson(data, MyOrderBean.DataBean.class);
        chantime.setText("创建时间：" + goobean.getCreateTime());
        ordercode.setText("订单号：" + goobean.getOrderNo());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        time = DateUtils.handleDateString(goobean.getCreateTime(),7);
        Date date = new Date(System.currentTimeMillis());
        Log.e("time", time + "kong");
        Log.e("xitong", simpleDateFormat.format(date) + "");
        if(!EmptyUtil.isEmpty(time)){
            if("null".equals(time)){
                daojishi.setText(View.GONE);
            }else{
                if(EmptyUtil.isEmpty(cha(time, simpleDateFormat.format(date)))){
                    daojishi.setText(View.GONE);
                }else{
                    if(cha(time, simpleDateFormat.format(date)).contains("-")){
                        if(null!=daojishi){
                           // daojishi.setText(View.GONE);
                            daojishi.setText("");
                        }

                    }else{
                        daojishi.setText("剩余"+cha(time, simpleDateFormat.format(date) + "订单自动关闭"));
                    }

                }

            }

        }else{
            daojishi.setText(View.GONE);
        }

        if (goobean.getOrderType().equals("1")) {
            name.setText(goobean.getOrderInname());
            phone.setText(goobean.getOrderInphone());
            adress.setText(goobean.getOrderInaddress());
            ingongsi = goobean.getChangeInstatusName();
            inwuliuid = goobean.getChangeIntrackno() + "";
            ingongsibainam = goobean.getChangeIntracknamecode() + "";
        } else if (goobean.getOrderType().equals("0")) {
            name.setText(goobean.getOrderOutname() + "");
            phone.setText(goobean.getOrderOutphone() + "");
            adress.setText(goobean.getOrderOutaddress() + "");

            ingongsi = goobean.getChangeOuttrackname() + "";
            inwuliuid = goobean.getChangeOuttrackno() + "";
            ingongsibainam = goobean.getChangeIntracknamecode() + "";
        }

        if (goobean.getOrderType().equals("1")) {
            outname.setText(goobean.getOrderOutname() + "");
            outtphone.setText(goobean.getOrderOutphone() + "");
            outadress.setText(goobean.getOrderOutaddress() + "");

            outgongsi = goobean.getChangeOuttrackname() + "";
            outwuliid = goobean.getChangeOuttrackno() + "";
            outgongsibianma = goobean.getChangeIntracknamecode() + "";


        } else if (goobean.getOrderType().equals("0")) {
            outname.setText(goobean.getOrderInname());
            outtphone.setText(goobean.getOrderInphone());
            outadress.setText(goobean.getOrderInaddress());

            outgongsi = goobean.getChangeInstatusName();
            outwuliid = goobean.getChangeIntrackno() + "";
            outgongsibianma = goobean.getChangeIntracknamecode() + "";
        }

        mGoodAdapter = new BaseQuickAdapter<MyOrderBean.DataBean.JjyhOrderGoodsListBean, BaseViewHolder>(R.layout.item_good, goobean.getJjyhOrderGoodsList()) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MyOrderBean.DataBean.JjyhOrderGoodsListBean items) {
                helper.setText(R.id.name, items.getGoodsTitle());
                helper.setText(R.id.money, items.getPrice() + "");
                helper.setText(R.id.tottle, "共" + items.getNum() + "件商品  总价值：¥" + BigDecimalUtil.decimalTwo(items.getPrice() * items.getNum()));
                String[] split = items.getGoodsPic().split("\\|");
                Glide.with(thisActivity).load(split[0]).into(helper.<ImageView>getView(R.id.logo));

                if("0".equals(items.getGoodsType())){
                    helper.setImageResource(R.id.huan,R.mipmap.ic_ru);
                    //换入
                }else if("1".equals(items.getGoodsType())){
                    //换出
                    helper.setImageResource(R.id.huan,R.mipmap.ic_chu);
                }
            }
        };
        goodlist.setLayoutManager(new LinearLayoutManager(thisActivity));
        goodlist.setAdapter(mGoodAdapter);

        if (goobean.getOrderType().equals("0")) {
            if (goobean.getOrderInstatus().equals("0")) {
                orderdeesc.setText("待回复");
                sattte.setText("待回复");
                bntright.setText("提醒回复");
            } else if (goobean.getOrderInstatus().equals("1")) {
                orderdeesc.setText("待确认");
                sattte.setText("待确认");
                bntright.setText("确认");
            } else if (goobean.getOrderInstatus().equals("2")) {
                orderdeesc.setText("待发货");
                sattte.setText("待发货");
                //0未确认 1未缴纳，2已缴纳 -1已退回
                if (goobean.getChangeInstatus().equals("0")) {
                    bntright.setText("支付保证金");
                } else if (goobean.getChangeInstatus().equals("1")) {
                    bntright.setText("支付保证金");
                } else if (goobean.getChangeInstatus().equals("2")) {
                    bnttleft.setVisibility(View.GONE);
                    if (EmptyUtil.isEmpty(goobean.getChangeIntracknamecode())) {
                        bntright.setText("填写物流");
                    } else {
                        bntright.setVisibility(View.GONE);
                    }

                }
            } else if (goobean.getOrderInstatus().equals("3")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setText("已收货");
                sattte.setText("待收货");
                orderdeesc.setText("待收货");
            } else if (goobean.getOrderInstatus().equals("4")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("已完成");
                sattte.setText("已完成");
            } else if (goobean.getOrderInstatus().equals("5")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("关闭");
                sattte.setText("关闭");
            } else if (goobean.getOrderInstatus().equals("6")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("已收货");
                sattte.setText("已收货");
            }
        } else if (goobean.getOrderType().equals("1")) {
            if (goobean.getOrderOutstatus().equals("0")) {
                orderdeesc.setText("待回复");
                sattte.setText("待回复");
                bntright.setText("提醒回复");
            } else if (goobean.getOrderOutstatus().equals("1")) {
                orderdeesc.setText("待确认");
                sattte.setText("待确认");
                bntright.setText("确认");
            } else if (goobean.getOrderOutstatus().equals("2")) {
                orderdeesc.setText("待发货");
                sattte.setText("待发货");
                if (goobean.getChangeOutstatus().equals("0")) {
                    bntright.setText("支付保证金");
                } else if (goobean.getChangeOutstatus().equals("1")) {
                    bntright.setText("支付保证金");
                } else if (goobean.getChangeOutstatus().equals("2")) {
                    bnttleft.setVisibility(View.GONE);

                    if (EmptyUtil.isEmpty(goobean.getChangeOuttracknamecode())) {
                        bntright.setText("填写物流");
                    } else {
                        bntright.setVisibility(View.GONE);
                    }

                }
            } else if (goobean.getOrderOutstatus().equals("3")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setText("已收货");
                orderdeesc.setText("待收货");
                sattte.setText("待收货");
            } else if (goobean.getOrderOutstatus().equals("4")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("已完成");
                sattte.setText("已完成");
            } else if (goobean.getOrderOutstatus().equals("5")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("关闭");
                sattte.setText("关闭");
            } else if (goobean.getOrderOutstatus().equals("6")) {
                bnttleft.setVisibility(View.GONE);
                bntright.setVisibility(View.GONE);
                orderdeesc.setText("已收货");
                sattte.setText("已收货");
            }

        }


    }

    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.reinfoback, R.id.wuliuyi, R.id.bntright, R.id.bnttleft, R.id.wuliuer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.reinfoback:
                this.finish();
                break;
            case R.id.wuliuyi:
                Bundle bundle = new Bundle();
                bundle.putString("gongsi", ingongsi);
                bundle.putString("code", ingongsibainam);
                bundle.putString("orderno", inwuliuid);
                ActivityUtils.startActivity(bundle, SeekWuliuActivity.class);
                break;
            case R.id.bntright:
                if (goobean.getOrderType().equals("0")) {
                    if (goobean.getOrderInstatus().equals("0")) {
                        //IM
                        ToastUtils.showShort(thisActivity,"提醒回复成功");
                    } else if (goobean.getOrderInstatus().equals("1")) {
                        //确认
                        Bundle bundlequeren = new Bundle();
                        bundlequeren.putString("id", goobean.getId());
                        ActivityUtils.startActivity(bundlequeren, QuernOrderActtivity.class);
                    } else if (goobean.getOrderInstatus().equals("2")) {

                        if (goobean.getChangeInstatus().equals("0")) {
                            //支付
                            zhifu(goobean.getId(), userId);
                        } else if (goobean.getChangeInstatus().equals("1")) {
                            //支付
                            zhifu(goobean.getId(), userId);
                        } else if (goobean.getChangeInstatus().equals("2")) {
                            if (EmptyUtil.isEmpty(goobean.getChangeIntracknamecode())) {
                                Bundle bundletianxie = new Bundle();
                                bundletianxie.putString("id", goobean.getId());
                                ActivityUtils.startActivity(bundletianxie, WuliuActtivity.class);
                            } else {

                            }

                        }
                    } else if (goobean.getOrderInstatus().equals("3")) {

                    } else if (goobean.getOrderInstatus().equals("4")) {

                    } else if (goobean.getOrderInstatus().equals("5")) {

                    } else if (goobean.getOrderInstatus().equals("6")) {

                    }
                } else if (goobean.getOrderType().equals("1")) {
                    if (goobean.getOrderOutstatus().equals("0")) {
                        //IM
                        ToastUtils.showShort(thisActivity,"提醒回复成功");

                    } else if (goobean.getOrderOutstatus().equals("1")) {
                        //确认
                        Bundle bundleq = new Bundle();
                        bundleq.putString("id", goobean.getId());
                        ActivityUtils.startActivity(bundleq, QuernOrderActtivity.class);
                    } else if (goobean.getOrderOutstatus().equals("2")) {

                        if (goobean.getChangeOutstatus().equals("0")) {
                            //支付
                            zhifu(goobean.getId(), userId);
                        } else if (goobean.getChangeOutstatus().equals("1")) {
                            //支付
                            zhifu(goobean.getId(), userId);
                        } else if (goobean.getChangeOutstatus().equals("2")) {
                            if (EmptyUtil.isEmpty(goobean.getChangeOuttracknamecode())) {
                                //物流
                                Bundle bundlec = new Bundle();
                                bundlec.putString("id", goobean.getId());
                                ActivityUtils.startActivity(bundlec, WuliuActtivity.class);
                            } else {

                            }
                        }

                    } else if (goobean.getOrderOutstatus().equals("3")) {

                    } else if (goobean.getOrderOutstatus().equals("4")) {

                    } else if (goobean.getOrderOutstatus().equals("5")) {

                    } else if (goobean.getOrderOutstatus().equals("6")) {

                    }

                }

                break;
            case R.id.bnttleft:

                delete(userId, goobean.getId());
                break;
            case R.id.wuliuer:
                Bundle bundles = new Bundle();
                bundles.putString("gongsi", outgongsi);
                bundles.putString("code", outgongsibianma);
                bundles.putString("orderno", outwuliid);
                ActivityUtils.startActivity(bundles, SeekWuliuActivity.class);
                break;
        }
    }

    /**
     * 详情
     */
    public void info(String userid, String id) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getOrderById(userid, id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("info", JsonStr);
                        Gson gson = new Gson();
                        XiangqingBean bean = gson.fromJson(JsonStr, XiangqingBean.class);


                    }
                });

    }

    /**
     * 支付
     */

    public void zhifu(String orderid, String userid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .wxPay(orderid, userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("JsonStr", JsonStr + "");
                        Gson gson = new Gson();
                        WEiXInPayBean wEiXInPayBean = gson.fromJson(JsonStr, WEiXInPayBean.class);
                        api.registerApp(wEiXInPayBean.getData().getAppid());
                        PayReq request = new PayReq();
                        request.appId = wEiXInPayBean.getData().getAppid();
                        request.partnerId = wEiXInPayBean.getData().getPartnerid();
                        request.prepayId = wEiXInPayBean.getData().getPrepayid();
                        request.nonceStr = wEiXInPayBean.getData().getNoncestr();
                        request.timeStamp = wEiXInPayBean.getData().getTimestamp();
                        request.packageValue = "Sign=WXPay";
                        request.sign = wEiXInPayBean.getData().getSign();
                        request.extData = "app data"; // optional
                        api.sendReq(request);
                        thisActivity.finish();
                    }
                });
    }

    /**
     * 删除
     */
    public void delete(String userid, String oredeid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .delOrder(userid, oredeid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        thisActivity.finish();

                    }
                });
    }

    private String tian ,sshi;
    private boolean zf;
    public String cha(String bigtime, String smailetime) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(bigtime);
            Date d2 = df.parse(smailetime);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            tian=days+"";
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            xiaoshi = hours + "";
            fenzhong = minutes + "";

            System.out.println("" + days + "天" + hours + "小时" + minutes + "分");

        } catch (Exception e) {
        }

        return tian +"天"+ xiaoshi+"小时" + fenzhong+"分钟";
    }
}
